#!/usr/bin/python3
# -*- coding: UTF-8 -*- 

import time  # 引入time模块

import smtplib
from email.mime.text import MIMEText
from email.header import Header

import myconfig         #配置类
 


mail_host = myconfig.mail_host              #设置服务器
mail_port = myconfig.mail_port              #配置端口
mail_user = myconfig.mail_user              #用户名
mail_pass = myconfig.mail_pass              #口令
sender    = myconfig.sender                 #发送者邮箱
receivers = myconfig.receivers              #接收者邮箱



def send_IPAddr_mail(newip):  
    time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())         #获取系统时间

    message = MIMEText('测试password：' + mail_pass + '\r\nnewip:' + newip + '\r\nTime:' +time_str, 'plain', 'utf-8')  #邮件内容
    message['From'] = Header("树莓派小哨兵", 'utf-8')                       #发件人
    message['To'] =  Header("~小宋的邮箱~", 'utf-8')                        #收件人
     
    subject = '树莓派IP更新'                      
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, mail_port)    # 25 为 SMTP 端口号
        smtpObj.login(mail_user,mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")